package com.open.capacity.controller;

import com.open.capacity.common.web.Result;
import com.open.capacity.model.CollectionStation;
import com.open.capacity.model.UserCollection;
import com.open.capacity.model.UserModel;
import com.open.capacity.service.CollectionService;
import com.open.capacity.util.PageUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: y_022
 * @date: 19:35-2021/6/23
 * @description:关于用户收藏操作的controller！
 */
@Slf4j
@Api(tags = "COLLECTION API")
@RestController
@RequestMapping("/collection")
public class CollectionController {
    @Autowired
    private CollectionService collectionService;

    /**
     * 按用户id查询用户的工位收藏表
     */
    @GetMapping("/findUserCollectionListByUserId")
    public Result findUserCollectionListByUserId(UserModel userModel,int page,int limit) {
        userModel.setLimits(limit);
        userModel.setPagees(page);
        List<UserCollection> userCollectionList = collectionService.findUserCollectionListByUserId(userModel);
        ArrayList<CollectionStation> objects = new ArrayList<>();
        for (UserCollection userCollection : userCollectionList) {
            CollectionStation result = collectionService.findUserCollectionStationsByStationId(userCollection);
            objects.add(result);
        }
        List list = PageUtils.startPage(objects, userModel.getPagees(), userModel.getLimits()); //直接对集合分页,返回一个已经分页list
        return Result.succeed(list, "操作成功!");
    }

    @GetMapping("/cancelCollection")
    public Result cancelCollection(UserCollection userCollection){
    try{
      collectionService.cancelCollection(userCollection);
      return Result.succeed("操作成功！");
    }catch (Exception e){
        log.error("操作失败！",e);
    }
    return  Result.failed("操作失败！");
    }
}
